/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mbean.admin.driver;

import mbean.admin.customer.*;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlInputText;
import mbean.AbstractMB;
import model.entity.Customer;
import model.entity.Depot;
import model.entity.Driver;
import model.entity.Role;
import model.entity.User;
import model.entity.Vehicle;
import org.omnifaces.util.Faces;
import org.omnifaces.util.Messages;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.model.LazyDataModel;
import service.IUserService;
import service.InterfaceServiceLocal;
import utils.HttpmethodUtil;
import utils.QueryStr;

/**
 *
 * @author dingnangen
 */
@Named(value = "driverCreateMB")
@SessionScoped
public class DriverCreateMB extends AbstractMB implements Serializable {

    @EJB(beanName = "DriverManager")
    private InterfaceServiceLocal<Driver> driverManager;
    @EJB(beanName = "VehicleManager")
    private InterfaceServiceLocal<Vehicle> vehicleManager;
    @EJB(beanName = "RoleManager")
    private InterfaceServiceLocal<Role> roleManager;
    @EJB(beanName = "UserManager")
    private IUserService<User> userManager;
    @EJB(beanName = "DepotManager")
    private InterfaceServiceLocal<Depot> depotManager;
    private Driver currentdriver;

    private User currentUser;
    private List<Vehicle> vehiclelist;
    private List<Depot> depotlist;

    /**
     * 初始化
     */
    public void init() {
        depotlist = depotManager.findAll();
        initvehiclelist();
        this.currentdriver = new Driver();
        currentUser = new User();
        currentUser.setPassword("21218CCA77804D2BA1922C33E0151105");
        currentUser.setLastLogin(new Date());
        List<Role> roleList = new ArrayList();
        Role role = roleManager.find(2);
        System.out.println(role.getRoleName());
        roleList.add(role);
        currentUser.setRoleList(roleList);
        this.currentdriver.setIduser(currentUser);

    }

    public void precreateDriver() {
        if (Faces.isPostback()) {
            System.out.println("isPostback");
            return;
        }
        init();
    }

    public void create() {
        if (!telvali()) {
            return;
        }
        try {
            userManager.create(currentUser);
//            currentdriver.setIdvehicle(vehicleManager.find(Integer.parseInt(currentvehicleid)));
//            currentdriver.setIddepot(depotManager.find(Integer.parseInt(currentiddepot)));
            driverManager.create(currentdriver);
                        HttpmethodUtil.addEntity(currentdriver.getIduser().getLoginname(),currentdriver.getIduser().getName());
            System.out.println("create finish");
        } catch (Exception e) {
            System.out.println("failed");
            Messages.create(Faces.getResourceBundle("bundle").getString("failed")).add();
        }
        Messages.create(Faces.getResourceBundle("bundle").getString("saveSuccess")).add();
        init();
    }

    /**
     * Creates a new instance of CustomerCreateMB
     */
    public DriverCreateMB() {
    }

    public Driver getCurrentdriver() {
        return currentdriver;
    }

    public void setCurrentdriver(Driver currentdriver) {
        this.currentdriver = currentdriver;
    }

    public boolean telvali() {
        List params = new ArrayList();
        QueryStr queryStr = new QueryStr("User", 0);
        queryStr.addLikeCondition("where", "tel");
        params.add(currentUser.getTel().replaceAll("\\s*", ""));
        User findOneItem = userManager.findOneItem(queryStr.getQueryList(), params);
        if (findOneItem != null) {
            Messages.create(Faces.getResourceBundle("bundle").getString("unique")).add();
            return false;
        }
        return true;
    }

    public void ondepotchange() {
        List p = new ArrayList();
        p.add(currentdriver.getIddepot());
        QueryStr queryStr = new QueryStr("Vehicle", 0);
        queryStr.addEqualCondition("where", "iddepot");
        vehiclelist = vehicleManager.findAll(queryStr.getQueryList(), p);
        System.out.println("ondepotchange");

    }

    public void initvehiclelist() {

        if (depotlist.isEmpty()) {
            return;
        }
        List p = new ArrayList();
        p.add(depotlist.get(0));
        QueryStr queryStr = new QueryStr("Vehicle", 0);
        queryStr.addEqualCondition("where", "iddepot");
        vehiclelist = vehicleManager.findAll(queryStr.getQueryList(), p);
    }

    public List<Vehicle> getVehiclelist() {

        return vehiclelist;
    }

    public void setVehiclelist(List<Vehicle> vehiclelist) {
        this.vehiclelist = vehiclelist;
    }

    public List<Depot> getDepotlist() {

        return depotlist;
    }

    public void setDepotlist(List<Depot> depotlist) {
        this.depotlist = depotlist;
    }

}
